﻿using System;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace MyWebPart
{
    [Guid("6e0152ef-b027-42dc-ac69-2c6904ac05b3")]
    public class MyWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        /// <summary>
        /// The color of the text to set in the child user control.
        /// </summary>
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [WebDisplayName("Text Color")]
        [WebDescription("Color of text in the main content of the Web Part.")]
        [SPWebCategoryName("Development")]
        public string TextColor
        {
            get;
            set;
        }


        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            try
            {
                // Loads a user control
                MyUserControl myUserControl = (MyUserControl)Page.LoadControl("~/_controltemplates/MyWebPart/MyUserControl.ascx");
                myUserControl.Web = SPContext.Current.Web;
                myUserControl.TextColor = this.TextColor;

                // Adds it to the controls collection of the Web Part 
                this.Controls.Add(myUserControl);
            }
            catch (HttpException ex)
            {
                this.Controls.Add(new LiteralControl("<br />An unexpected error occurred loading Web Part. " + ex.Message));
            }
        }
    }
}
